var Customer = {id: "CustomerTable", seItem: null, table: null, layerIndex: -1};

function _formatter(value, row, index) {
    if (row.accountArrears > 0) {
        return '<span style="color: red" title="' + value + '">' + value + '</span>'
    } else {
        return '<span title="' + value + '">' + value + '</span>'
    }
}

function _formatterTime(value, row, index) {
    if (row.accountArrears > 0) {
        return "<span style='color: red'>" + value.substring(0, 19) + "</span>"
    } else {
        return value.substring(0, 19)
    }
}

function _cellStyles(value, row, index) {
    if (row.customerStatus === "作废") {
        return {css: {"background-color": "#a29a88", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

Customer.initColumn = function () {
    var columns = [{field: 'selectItem', radio: true}, {
        title: 'id',
        field: 'id',
        visible: false,
        align: 'center',
        valign: 'middle'
    }, {
        title: 'reservationsId',
        field: 'reservationsId',
        visible: false,
        align: 'center',
        valign: 'middle'
    }, {
        title: 'customerSexHead',
        field: 'customerSexHead',
        visible: false,
        align: 'center',
        valign: 'middle'
    }, {
        title: '病历号',
        field: 'medicalRecordNumber',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 120,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '姓名',
        field: 'customerName',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 120,
        formatter: function (value, row, index) {
            if (row.accountArrears > 0) {
                if (row.orthodonticsStatus > 0) {
                    return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick = 'Customer.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span><img src=\"https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png\" alt=\"...\" width=\"18px;\"></a>"
                }else {
                    return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick = 'Customer.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            } else {
                if (row.orthodonticsStatus > 0) {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'Customer.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span><img src=\"https://pynnn.oss-cn-hangzhou.aliyuncs.com/ychh001.png\" alt=\"...\" width=\"18px;\"></a>"
                }else {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'Customer.openBilee(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            }
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '电话',
        field: 'customerPhone',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 135,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '电话2',
        field: 'customerPhone2',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 135,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '使用人',
        field: 'phoneSharShip',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 90,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '性别',
        field: 'sex',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 80,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '年龄',
        field: 'customerAge',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 80,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '科室',
        field: 'treatmentUnit',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 100,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '客户状态',
        field: 'customerStatus',
        align: 'center',
        valign: 'middle',
        width: 120,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '一级渠道',
        field: 'oneChannel',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 110,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '二级渠道',
        field: 'twoChannel',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 110,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '三级渠道',
        field: 'thrChannel',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 130,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '网电咨询师',
        field: 'scarchiving',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 130,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '开单状态',
        field: 'dealStatus',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 110,
        formatter: function (value, row, index) {
            if (value === "已结账") {
                if (row.accountArrears > 0) {
                    return "<a href='javascript:;' style='color: red' onclick='Customer.opendealStatus(" + '"' + row.reservationsId + '"' + ")'>[已结账]</a>"
                } else {
                    return "<a href='javascript:;' style='color: #1ab394' onclick='Customer.opendealStatus(" + '"' + row.reservationsId + '"' + ")'>[已结账]</a>"
                }
            } else if (value !== "已结账") {
                return _formatter(value, row, index)
            }
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '已交金额',
        field: 'actualAccountBalance',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 110,
        formatter: function (value, row, index) {
            if (row.accountArrears > 0) {
                return "<a href='javascript:;' style='color: red'>" + (row.actualAccountBalance + row.moneyBalance+ parseFloat(row.orthodonticsStatus)) + "</a>"
            } else {
                return "<a href='javascript:;'>" + (row.actualAccountBalance + row.moneyBalance+ parseFloat(row.orthodonticsStatus)) + "</a>"
            }
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '欠费',
        field: 'accountArrears',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 100,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '现场TC',
        field: 'fieldTc',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 100,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '医生',
        field: 'doctor',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 100,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '到店时间',
        field: 'arrivalTime',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 170,
        formatter: function (value, row, index) {
            return _formatterTime(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '初诊时间',
        field: 'firstVisitDate',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 110,
        formatter: function (value, row, index) {
            return _formatterTime(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '黄页编号',
        field: 'yellowNumber',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 140,
        formatter: function (value, row, index) {
            return _formatterTime(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }, {
        title: '备注',
        field: 'remarks',
        align: 'center',
        valign: 'middle',
        sortable: true,
        width: 120,
        formatter: function (value, row, index) {
            return _formatter(value, row, index)
        },
        cellStyle: function (value, row, index) {
            return _cellStyles(value, row, index)
        }
    }];
    return columns
};
Customer.formParams = function () {
    var queryData = {};
    queryData['sreach'] = $("#sreach").val();
    queryData['treatmentUnit'] = $("#treatmentUnit").val();
    queryData['customerName'] = $("#customerName").val();
    queryData['customerPhone'] = $("#customerPhone").val();
    queryData['startArrivalTime'] = $("#startArrivalTime").val();
    queryData['endArrivalTime'] = $("#endArrivalTime").val();
    queryData['reservations'] = $("#reservations").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    queryData['condition'] = $("#condition").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    queryData['oneChannel'] = multipleSelection($('#oneChannel').val());
    queryData['twoChannel'] = multipleSelection($('#twoChannel').val());
    queryData['thrChannel'] = multipleSelection($('#thrChannel').val());
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData
};
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i)
    });
    return paramStr.substr(1)
};
var getSingleData = function (param, key) {
    return key + "=" + param[key]
};
Customer.updateChildrenCard = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '设置无龋齿卡',
            area: ['40%', '50%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/updateChildrenCard/' + Customer.seItem.id
        });
        this.layerIndex = index
    }
};

Customer.updateTime = function () {
    if (this.check()) {
        var id = Customer.seItem.id;
        var index = layer.open({
            type: 2,
            title: '修改时间',
            area: ['40%', '50%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/update_time/' + id
        });
        this.layerIndex = index
    }
};
Customer.updateBelonger = function () {
    if (this.check()) {
        var id = Customer.seItem.id;
        var index = layer.open({
            type: 2,
            title: '修改时间',
            area: ['80%', '70%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/update_belonger/' + id
        });
        this.layerIndex = index
    }
};
Customer.updateTimes = function () {
    var updateTime = $("#updateTime").val();
    var customerId = $("#customerId").val();
    $.ajax({
        url: Feng.ctxPath + "/customer/updateTime",
        type: "post",
        data: {"customerId": customerId, "updateTime": updateTime,},
        success: function (data) {
            Feng.success(" 修改结账时间成功！");
            window.parent.Customer.table.refresh();
            Customer.close()
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
Customer.opendealStatus = function (id) {
    $.ajax({
        url: Feng.ctxPath + "/reservations/getTotleMoneys",
        type: "post",
        data: {"id": id},
        success: function (data) {
            alert("成交金额：" + data + "元")
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
};
Customer.openBilee = function (ide) {
    $.ajax({
        url: Feng.ctxPath + "/consultantTwo/getdcsByCustomerId",
        type: "post",
        data: {"id": ide},
        success: function (data) {
            if (data == 1) {
                var index = layer.open({
                    type: 2,
                    title: '到店客户详情',
                    area: ['100%', '100%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + ide + "/0"
                });
                this.layerIndex = index
            } else {
                alert("请先联系前台客服匹配建档！");
                return
            }
        }
    })
};
Customer.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name))
};
Customer.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false
    } else {
        Customer.seItem = selected[0];
        return true
    }
};
Customer.search = function () {
    Customer.table.refresh({query: Customer.formParams()});
    Customer.total()
};
Customer.total = function () {
    var startArrivalTime = $("#startArrivalTime").val();
    var endArrivalTime = $("#endArrivalTime").val();
    var valueOne = multipleSelection($('#oneChannel').val());
    var valueTwo = multipleSelection($('#twoChannel').val());
    var valueThr = multipleSelection($('#thrChannel').val());
    var dealStatus = $("#dealStatus").val();
    var fieldTc = $("#fieldTc").val();
    var treatmentUnit = $("#treatmentUnit").val();
    var sreach = $("#sreach").val();
    var condition = 1;
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/customer/total",
        data: {
            startArrivalTime: startArrivalTime,
            endArrivalTime: endArrivalTime,
            oneChannelValue: valueOne,
            twoChannelValue: valueTwo,
            thrChannelValue: valueThr,
            fieldTc: fieldTc,
            treatmentUnit: treatmentUnit,
            sreach: sreach,
            condition: condition,
            dealStatus: dealStatus
        },
        dataType: "json",
        success: function (data) {
            $("#total").html(data[0].countNum)
        }
    })
};
Customer.updateTreatmentUnit = function () {
    if (this.check()) {
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + Customer.seItem.id,
            type: "post",
            success: function (data) {
                if (data == 1) {
                    alert("此患者已开单不能修改科室！");
                    return
                } else {
                    var index = layer.open({
                        type: 2,
                        title: '修改科室',
                        area: ['50%', '60%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/customer/update_TreatmentUnit/' + Customer.seItem.id
                    });
                    this.layerIndex = index
                }
            },
            error: function (data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }
};
Customer.batchChoiceTc = function () {
    var index = layer.open({
        type: 2,
        title: '选择Tc',
        area: ['50%', '60%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/customer/to_batchChoiceTc'
    });
    this.layerIndex = index
};
Customer.updateDoctor = function () {
    if (this.check()) {
        var id = Customer.seItem.id;
        var index = layer.open({
            type: 2,
            title: '选择医生',
            area: ['50%', '60%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/to_updateDoctor/' + id
        });
        this.layerIndex = index
    }
};
Customer.updateTochoiceTc = function () {
    if (this.check()) {
        var id = Customer.seItem.id;
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + id,
            type: "post",
            success: function (data) {
                if (data == 1) {
                    alert("此患者已开单不能修改TC！");
                    return
                } else {
                    var index = layer.open({
                        type: 2,
                        title: '选择Tc',
                        area: ['50%', '60%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/customer/to_choiceTc/' + id
                    });
                    this.layerIndex = index
                }
            },
            error: function (data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(windowNameIndex)
            }
        })
    }
};
Customer.updateCustomeInfo = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改个人信息',
            area: ['70%', '90%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/update_CustomerInfo/' + Customer.seItem.id
        });
        this.layerIndex = index
    }
};
Customer.updateMedium = function () {
    if (this.check()) {
        $.ajax({
            url: Feng.ctxPath + "/customer/isMarket",
            type: "post",
            data: {"oneChannel": Customer.seItem.oneChannel},
            success: function (data) {
                if (data == 1) {
                    alert('不是自然到店的不能修改!')
                } else {
                    var index = layer.open({
                        type: 2,
                        title: '修改媒介',
                        area: ['50%', '60%'],
                        fix: true,
                        maxmin: true,
                        content: Feng.ctxPath + '/customer/update_medium/' + Customer.seItem.id
                    });
                    this.layerIndex = index
                }
            }
        })
    }
};
Customer.updatePhone = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改电话',
            area: ['40%', '50%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/customer/update_phone/' + Customer.seItem.id
        });
        this.layerIndex = index
    }
};
Customer.openToVoid = function () {
    if (this.check()) {
        $.ajax({
            url: Feng.ctxPath + "/customer/getCustomerIsDealStatus/" + Customer.seItem.id,
            type: "post",
            success: function (data) {
                if (data == 1) {
                    alert("此患者已开单不能作废！");
                    return
                } else if (data == 3) {
                    alert("此患者为初诊不能作废！");
                    return
                } else if (data == 4) {
                    alert("此患者充了储值或积分不能作废！");
                    return
                } else {
                    $.ajax({
                        url: Feng.ctxPath + "/customer/is_void",
                        type: "post",
                        data: {"customerId": Customer.seItem.id},
                        success: function (data) {
                            if (data.dealStatus == '已结账' || data.dealStatus == '已开单') {
                                alert('该患者已经结账或开单,不能作废!');
                                return
                            } else if (data.moneyBalance > 0) {
                                alert('该患者充了储值或积分,不能作废!');
                                return
                            } else {
                                var index = layer.open({
                                    type: 2,
                                    title: '作废备注',
                                    area: ['50%', '60%'],
                                    fix: true,
                                    maxmin: true,
                                    content: Feng.ctxPath + '/customer/openToVoid/' + Customer.seItem.id
                                });
                                this.layerIndex = index
                            }
                        }
                    })
                }
            }
        })
    }
};

Customer.time = function () {
    $("#startArrivalTime").val(setDateTime(new Date(), 0) + " 00:00:00");
    $("#endArrivalTime").val(setDateTime(new Date(), 0) + " 23:59:59")
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#CustomerTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}

$(function () {
    var defaultColunms = Customer.initColumn();
    var table = new BSTable(Customer.id, "/customer/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Customer.formParams());
    Customer.table = table.init();
    Customer.time();
    Customer.search();
    menuNameHideColumn($("#menuName").val(),'CustomerTable');
    $("#oneChannel").change(function () {$("#twoChannel").val('请选择');$("#thrChannel").val('请选择');Customer.search()});
    $("#twoChannel").change(function () {$("#thrChannel").val('请选择');Customer.search()});
    $("#thrChannel").change(function () {Customer.search()});
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {Customer.search()});
    checkDate = function () {Customer.search()};
    phonePrivileges();
});